package test;

public class Queue2 {
    //环形队列
    //数组
    String[]array;
    //队头指针
    int head=0;
    //队尾指针
    int tail=0;
    public Queue2(int max){
        array = new String[max];
    }
    public void add(String key){
        //判断队列是否已满----不考虑队尾在数组最后一个下标bug
       /* if(tail+1==head){
            if(array[tail]!=null) {
                throw new RuntimeException("队已满");
            }
        }*/
        if(tail==head&&array[tail]!=null){throw new RuntimeException("队已满");}
        //队尾赋值
        array[tail]=key;
        //队尾指针刷新

        tail=(tail+1)%array.length;

        
    }
    public void delect(){
        //判断队列是否为空
        if(head==tail){
            if(array[head]==null){
                throw new RuntimeException("队已空");
            }

        }
        array[head]=null;
        head=(head+1)%array.length;
    }
    public void show(){
        //查看当前元素个数
        System.out.println("当前队列元素个数为-->"+(tail-head+array.length)%array.length+"----"+"当前队头为array["+head+"]队尾下一个为array["+tail+"]");
        for (String i:array){
            System.out.println(i);
        }

    }
    public int getLength(){
        return (tail-head+array.length)%array.length;
    }
}
